package tq.com.tqteacher.activity.login;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.systools.system.StringHelper;
import tq.com.tqcom.base.BaseActivity;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetClient;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.util.TQLoadingHelper;
import tq.com.tqteacher.R;
import tq.com.tqteacher.activity.LaunchActivity;
import tq.com.tqteacher.model.TQLogin;
import tq.com.tqteacher.model.TQUserInfo;
import tq.com.tqteacher.net.TQNetInterface;
import tq.com.tqteacher.view.TQInputView1;
import tq.com.upload.TQUploadService;

public class TQLoginActivity extends BaseActivity {
    private TQInputView1 userName;
    private TQInputView1 pwd;
    private TextView mVersion;

    static public void show(Activity base){
        Intent intent = new Intent(base, TQLoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
        base.startActivity(intent);
        base.finish();
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_layout);
        setStatusBar();

        findViewById(R.id.login_phone).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TQPhoneLoginActivity.show(TQLoginActivity.this);
            }
        });

        findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
        userName = findViewById(R.id.user_name);
        pwd = findViewById(R.id.password);
        mVersion = findViewById(R.id.version);
        userName.setMaxLength(11);

        String name = TQUserInfo.getSaveName(this);
        String lPwd = TQUserInfo.getSavePwd(this);
        userName.setString(name);
        pwd.setString(lPwd);

        Intent intent = new Intent(getApplicationContext(), TQUploadService.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startService(intent);

        PackageManager lManager = getPackageManager();
        try {
            PackageInfo lInfo = lManager.getPackageInfo(getPackageName(), 0);
            mVersion.setText(String.format("班群 %s", lInfo.versionName));
        } catch (PackageManager.NameNotFoundException pE){
            pE.printStackTrace();
        }

        if(!StringHelper.isEmpty(name) && !StringHelper.isEmpty(lPwd)){
            login();
        }
    }

    private void login(){
        final String n = userName.getString();
        final String p = pwd.getString();

        if(StringHelper.isEmpty(n)){
            AlertDialogHelper.show(this, "用户名不能为空");
            return;
        }

        if(StringHelper.isEmpty(p)){
            AlertDialogHelper.show(this, "用户密码不能为空");
            return;
        }

        TQLoadingHelper.build(this).setCancelable(false).show();
        TQUserInfo.saveLoginInfo(this, n, p);
        TQNetInterface.login(n, p, new INetCallback() {
            @Override
            public void result(TQNetResult pNetResult) {
                Log.d(TAG, "result: ");
                if(!pNetResult.isSuccess()){
                    TQUserInfo.saveLoginInfo(TQLoginActivity.this, n, "");
                    TQLoadingHelper.setError(pNetResult.getErrMsg());
                    return;
                }

                TQLoadingHelper.dimiss();

                TQLogin.getCurrent().refresh(pNetResult.getData());
                gotoMain();
            }
        });
    }

    private void gotoMain(){
        LaunchActivity.show(TQLoginActivity.this);
    }
}
